<?php
/**
 * @Author:      lyt
 * @DateTime:  2017-05-18 15:17:26
 * @Description: 期初余额列表
 */
class BeginBalance extends Eloquent
{
    protected $table = "begin_balance";
    

    /**
     * @Author:      lyt
     * @DateTime:  2017-05-18 17:13:13
     * @Description: 门店划拨充值
     *
     */
    public static function rechange($bb_id,$user_id,$acount_type,$sc_user,$id_card){
        try{
            $response = DB::transaction(function() use ($bb_id,$user_id,$acount_type,$sc_user,$id_card){
                //查出期初记录
                $bb=BeginBalance::where("id",$bb_id)->lockForUpdate()->first();
                //检查是否有该期初划拨充值
                $temp_bbr=BeginBalanceRecharge::where("bg_id",$bb->id)->where("status",1)->first();
                if($temp_bbr){
                    DB::rollback();
                    return Lang::get("scs_error.bb_cant_recharge_error_40109");
                
                }
                //交易流水         

                $arr_id=Account::service_recharge($user_id,12,$acount_type,$bb->amount,'sc',$sc_user->id,$sc_user->shop_id);
                //记录起初充值记录
                $bbr=new BeginBalanceRecharge();
                $bbr->bg_id=$bb->id;
                $bbr->user_id=$user_id;
                $bbr->aurr_id=$arr_id;
                $bbr->amount=$bb->amount;
                $bbr->type=$acount_type;
                $bbr->created_by=$sc_user->id;
                $bbr->id_card=$id_card;               
                $bbr->save();                
                AcountUserRecharge::where("id",$arr_id)->update(array("pay_id"=>$bbr->id));
                return array('code' => 0, 'msg' => 'ok', 'result' => array("arr_id"=>$arr_id));
            });
            return $response;
        } catch (Exception $e) {
            Log::error("error", array("system" => "Account" ,"event" => "rechange" ,"exception" => $e));
            return false;
        }
    }
}